package org.uxy.plugin.gitclonehelper.ui

import com.intellij.openapi.ui.ValidationInfo
import com.intellij.openapi.vcs.CheckoutProvider
import com.intellij.openapi.vcs.ui.cloneDialog.VcsCloneDialogExtensionComponent
import com.intellij.ui.JBColor
import com.intellij.ui.util.maximumHeight
import okhttp3.HttpUrl
import okhttp3.OkHttpClient
import okhttp3.Request
import org.uxy.plugin.gitclonehelper.util.IconUtil
import java.awt.Dimension
import java.io.IOException
import javax.swing.BorderFactory
import javax.swing.Box
import javax.swing.BoxLayout
import javax.swing.JButton
import javax.swing.JComponent
import javax.swing.JLabel
import javax.swing.JPanel
import javax.swing.JTextField


class GitCloneHelperView : JComponent() {
    init {
        layout = BoxLayout(this, BoxLayout.Y_AXIS)
        add(gitList())
        add(Box.createRigidArea(Dimension(0, 10)))
        add(gitEdit())
        add(Box.createRigidArea(Dimension(0, 10)))
        add(account())
        add(Box.createRigidArea(Dimension(0, 10)))
        add(repositoryList())
        add(Box.createGlue())
        add(directory())
    }

    fun gitList(): JComponent {
        val panel = JPanel()
        panel.layout = BoxLayout(panel, BoxLayout.X_AXIS)
        panel.background = JBColor.RED
        val gitCode = JButton("GitCode", IconUtil.GIT_CODE_ICON)
        val gitEa = JButton("Gitea", IconUtil.GIT_EA_ICON)
        val gitEe = JButton("Gitee", IconUtil.GIT_EE_ICON)
        val gitHub = JButton("GitHub", IconUtil.GIT_HUB_ICON)
        val gitLab = JButton("GitLab", IconUtil.GIT_LAB_ICON)
        listOf(gitCode, gitEa, gitEe, gitHub, gitLab).forEach {
            panel.add(it)
        }
        return panel
    }

    fun gitEdit(): JComponent {
        val panel = JPanel()
        panel.layout = BoxLayout(panel, BoxLayout.X_AXIS)
        panel.background = JBColor.GREEN
        val label = JLabel("Git:")
        val edit = JTextField("https://gitcode.com")
        edit.border = BorderFactory.createLineBorder(JBColor.BLACK)
        edit.maximumHeight = 30
        panel.add(label)
        panel.add(Box.createRigidArea(Dimension(10, 0)))
        panel.add(edit)
        return panel
    }

    fun account(): JComponent {
        val panel = JPanel()
        panel.layout = BoxLayout(panel, BoxLayout.X_AXIS)
        panel.background = JBColor.BLUE
        val label = JLabel("Username:")
        val edit = JTextField("")
        edit.maximumHeight = 30
        val auth = JButton("Auth")
        panel.add(label)
        panel.add(Box.createRigidArea(Dimension(10, 0)))
        panel.add(edit)
        panel.add(Box.createRigidArea(Dimension(10, 0)))
        panel.add(auth)
        return panel
    }

    fun repositoryList(): JComponent {
        val panel = JPanel()
        panel.layout = BoxLayout(panel, BoxLayout.Y_AXIS)
        panel.add(JLabel("Repository List"))
        for (i in 0..10) {
            panel.add(JLabel("Repository $i"))
        }
        return panel
    }

    fun directory(): JComponent {
        val panel = JPanel()
        panel.layout = BoxLayout(panel, BoxLayout.X_AXIS)
        panel.background = JBColor.PINK
        val label = JLabel("Dist:")
        val edit = JTextField("/home/foo/bar")
        edit.maximumHeight = 30
        panel.add(label)
        panel.add(Box.createRigidArea(Dimension(10, 0)))
        panel.add(edit)
        return panel
    }
}

class GitCloneHelperComponent : VcsCloneDialogExtensionComponent() {

    override fun doClone(checkoutListener: CheckoutProvider.Listener) {
        println("doClone")
    }

    override fun doValidateAll(): List<ValidationInfo> {
        return listOf()
    }

    override fun getView(): JComponent {
        return GitCloneHelperView()
    }

    override fun onComponentSelected() {
        println("onComponentSelected")
        val client = OkHttpClient()
        val url = HttpUrl.Builder()
            .scheme("https")
            .host("api.gitcode.com")
            .addPathSegment("api")
            .addPathSegment("v5")
            .addPathSegment("user")
            .addPathSegment("repos")
            .addQueryParameter("access_token", "").build()
        println("URL: $url")
        val request = Request.Builder()
            .url(url)
            .get()
            .build()
        try {
            client.newCall(request).execute().use { response ->
                if (response.isSuccessful) {
                    println(response.body?.string())
                } else {
                    println("Request failed: ${response.code} - ${response.message}")
                    response.body?.string()?.let { responseBody ->
                        println("Response body: $responseBody")
                    }
                }
            }
        } catch (e: IOException) {
            println("An error occurred while making the request: ${e.message}")
            e.printStackTrace()
        }
    }
}
